package com.example.partyplaner;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.os.Build;

public class SingleTaskActivity extends Activity implements AsyncResponse {

	TextView titleTextView;
	TextView descriptionTextView;
	CheckBox isDoneBox;
	int taskId;
	DatabaseConnection db;
	private String responseMsg;
	private int responseCode;
	int logout=0;
	String filename="SingleTask";
	FileOutputStream outputStream;
	FileInputStream inputStream;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		EventTask taskRecived = null;
		if (extras != null) {
			taskRecived = (EventTask) extras.getSerializable("TASK_ID");
		}
		setContentView(R.layout.activity_single_task);
		titleTextView = (TextView) findViewById(R.id.textView1);
		descriptionTextView = (TextView) findViewById(R.id.textView2);
		isDoneBox = (CheckBox) findViewById(R.id.checkBox1);

		if (taskRecived.title != "No tasks available") {
			titleTextView.setText(taskRecived.getTitle());
			descriptionTextView.setText(taskRecived.getDescription());
			if (taskRecived.done_made == 1)
				isDoneBox.setChecked(true);
			else
				isDoneBox.setChecked(false);
		} else {
			titleTextView.setText(taskRecived.getTitle());
			descriptionTextView.setVisibility(View.INVISIBLE);
			isDoneBox.setVisibility(View.INVISIBLE);
		}
		taskId=taskRecived.getTaskID();
		
		
		isDoneBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isDoneBox.isChecked()) {
					 db = new DatabaseConnection(SingleTaskActivity.this);
					 db.delegate = SingleTaskActivity.this;
					 db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/MarkAsDone/"+MainActivity.token, "GET",Integer.toString(taskId));		
				
				} else {
					 db = new DatabaseConnection(SingleTaskActivity.this);
					 db.delegate = SingleTaskActivity.this;
					 db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/MarkAsDone/"+MainActivity.token, "GET", Integer.toString(taskId));		
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.single_task, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.logout) {
			this.db = new DatabaseConnection(SingleTaskActivity.this);
			this.db.delegate = SingleTaskActivity.this;
			db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/Logout", "GET", MainActivity.token);
			logout=1;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void processFinish(WebserviceResponse output) {
		if(output!=null){
			responseMsg=output.getResponseMsg();
			responseCode=output.getResponseCode();
			if (responseCode==200){
				if (logout!=1)
				{
					Log.i("PartyPlanner", "Code 200");
					Log.i("PartyPlanner", responseMsg);
				}
				else{
					Intent intent = new Intent(getApplicationContext(),MainActivity.class);
					startActivity(intent);
				}
			}
			else{
				Log.i("PartyPlanner", "Fail");
			}	
		}
		else{
			Log.i("PartyPlanner", "NULL");
		}
	}

	public Dialog onCreateDialog(int dialogId) {
		switch (dialogId) {
		case MainActivity.PLEASE_WAIT_DIALOG:
			ProgressDialog dialog = new ProgressDialog(SingleTaskActivity.this);
			dialog.setTitle("Connecting");
			dialog.setMessage("Please wait....");
			dialog.setCancelable(true);
			return dialog;
		default:
			break;
		}
		return null;
	}

}
